package com.myf.services;

import com.myf.AppException;
import com.myf.PaginationResult;
import com.myf.Utility;
import com.myf.dtos.SelectItem;
import com.myf.dtos.material.MaterialInput;
import com.myf.dtos.material.MaterialQueryInput;
import com.myf.dtos.material.MaterialQueryOutput;
import com.myf.entities.MaterialEntity;
import com.myf.mappers.MaterialMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class MaterialService {
    private final MaterialMapper materialMapper;

    public MaterialService(MaterialMapper materialMapper) {
        this.materialMapper = materialMapper;
    }

    public int save(MaterialInput input, int currentUserId) throws AppException {
        if (input.id == null || input.id == 0) {
            // 检查名称是否重复
            var map = new HashMap<String, Object>();
            map.put("name", input.name);
            if (materialMapper.isNameExists(map) == 1) {
                throw new AppException("name ".concat(input.name).concat(" already exists"));
            }
            // 插入数据库
            var entity = new MaterialEntity();
            entity.attributes = "{}";
            setToEntity(input, entity);
            entity.createAt = Utility.getCurrentUTCTime();
            entity.createBy = currentUserId;
            entity.version = 0;
            return materialMapper.insert(entity);
        } else {
            // 检查名称是否重复
            var map = new HashMap<String, Object>();
            map.put("id", input.id);
            map.put("name", input.name);
            if (materialMapper.isNameExists(map) == 1) {
                throw new AppException("name ".concat(input.name).concat(" already exists"));
            }
            // 更新数据库
            var entity = this.materialMapper.selectById(input.id);
            if (entity == null) {
                throw new AppException("the warehouse info is invalid");
            }
            setToEntity(input, entity);
            entity.updateAt = Utility.getCurrentUTCTime();
            entity.updateBy = currentUserId;
            return materialMapper.updateById(entity);
        }
    }

    private static void setToEntity(MaterialInput input, MaterialEntity entity) {
        entity.code = input.code;
        entity.name = input.name;
        entity.desc = input.desc;
        entity.category = input.category;
        entity.spec = input.spec;
        entity.height = input.height;
        entity.width = input.width;
        entity.length = input.length;
        entity.vendor = input.vendor;
        entity.measurementUnit = input.measurementUnit;
        entity.saftyQuantity = input.saftyQuantity;
        entity.alertQuantity = input.alertQuantity;
        entity.currentQuantity = input.currentQuantity;
        entity.temperature = input.temperature;
        entity.humidity = input.humidity;
        entity.status = input.status;
    }

    public PaginationResult<MaterialQueryOutput> query(MaterialQueryInput input) {
        var map = new HashMap<String, Object>();
        if (!input.keyword.isBlank()) {
            map.put("keyword", "%".concat(input.keyword).concat("%"));
        }
        map.put("limit", input.pageSize);
        map.put("offset", input.getOffset());
        var total = materialMapper.getPaginationCount(map);
        var rows = materialMapper.getPaginationList(map);
        return new PaginationResult<>(total, rows);
    }

    public MaterialEntity getById(int id) {
        return materialMapper.selectById(id);
    }

    /// 删除
    public int delete(List<Integer> ids, int currentUserId) {
        var hashMap = new HashMap<String, Object>();
        hashMap.put("ids", ids);
        hashMap.put("deleteBy", currentUserId);
        hashMap.put("deleteAt", Utility.getCurrentUTCTime());
        return materialMapper.removeByIds(hashMap);
    }

    public List<SelectItem> getForSelectItems(String keyword) {
        if (keyword != null && !keyword.isBlank()) {
            keyword = "%" + keyword + "%";
        }
        return materialMapper.getForSelectItems(keyword);
    }
}
